﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL.Interface;
using DAL.Models;

namespace DAL.Dao
{
    public class UserInfoDaoimpl : BaseDao, IUserDao
    {

        public int ExistNum { get { using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions)) return context.Manager.Count(); } private set { } }
        public UserInfoDaoimpl(bool use_clock = true) : base(use_clock)
        {
            
        }

        public bool Update(int id, UserInfo newitem)
        {
            using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions))
            {
                lock (clock_obj_update)
                {

                    UserInfo? item = (from i in context.UserInfo
                                     where i.Id == id
                                     select i).ToList().FirstOrDefault();
                    if (item == null)
                    {
                        return false;
                    }
                    //DbEntityEntry<Bag> entry = DbEntity.expressDBEntities.Entry<Bag>(newitem);
                    //3.设置该对象为被修改过
                    //entry.State = EntityState.Unchanged;
                    //4.设置该对象的 uName属性为修改状态，同时 entry.State 被修改为 Modified 状态
                    item.UserName = newitem.UserName;
                    //item.UserPass = newitem.UserPass;
                    item.UpdateTime = newitem.UpdateTime;
                    item.TrueName = newitem.TrueName;
                    item.Phone = newitem.Phone;
                    item.ContactAddress = newitem.ContactAddress;
                    item.Email = newitem.Email;
                    item.Idcard = newitem.Idcard;
                    item.UpdateTime = newitem.UpdateTime;
                    //item.AddTime = newitem.AddTime;


                    context.SaveChanges();

                    return true;
                }
            }
          
          
        }



        public IList<UserInfo> GetList()
        {
            using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions))
                return (from i in context.UserInfo orderby i.Id select i).ToList();
        }


        public (int,IList<UserInfo>) GetList(int rows , int page)
        {
            using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions))
            {
                var rs = (from i in context.UserInfo orderby i.Id select i).ToList();
                return (GetTotalPages(rs.Count, rows), rs.Skip((page - 1) * rows).Take(rows).ToList());
            }
             
        }

        public UserInfo? Login(string username, string userpass)
        {
            
            UserInfo? rs = null;
            try
            {
                using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions))
                    rs =  (from i in context.UserInfo
                        where i.UserName.Trim().Equals(username) && i.UserPass.Trim().Equals(userpass)
                        select i).ToList().FirstOrDefault();
            }
            catch (Exception e)
            {

                throw e.InnerException;
            }
            return rs;
        }


        public bool Delete(int id)
        {
            using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions))
                lock (clock_obj_delete)
            {
                
                UserInfo? user = context.UserInfo.Find(id);
                if (user == null) return false;
                context.UserInfo.Remove(user);
                context.SaveChanges();
                return true;
            }

        }
        public UserInfo Find(int id)
        {
            using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions))
                return context.UserInfo.Find(id);
           
        }

        public bool Add(UserInfo userInfo)
        {
            using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions))
                lock (clock_obj_add)
            {
                try
                {
                    
                    var res = (from i in context.UserInfo where i.UserName.Equals(userInfo.UserName) select i).FirstOrDefault();
                    if (res != null) return false;
                    //userInfo.Id = GetList().Last().Id + 1;
                    context.UserInfo.Add(userInfo);
                    context.SaveChanges();
                }
                catch (Exception)
                {

                    return false;
                }

                return true;
            }
           
        }
        public IList<UserInfo> GetUserInfo(int id)
        {
            using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions))
            {
                //使用Lambda表达式查询数据
                var info = (from i in context.UserInfo
                            where i.Id == id
                            select i).ToList();

                return info;
            }
               

        }
        public IList<UserInfo> GetUserInfo(string username)
        {
            using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions))
            {
                //使用Lambda表达式查询数据
                var info = (from i in context.UserInfo
                            where i.UserName.Equals(username)
                            select i).ToList();


                return info;
            }
               

        }


    }
}
